package cn.wolfcode.web.controller;


import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.feign.PayFeignApi;
import cn.wolfcode.service.IOrderInfoService;
import cn.wolfcode.web.msg.SeckillCodeMsg;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;


@RestController
@RequestMapping("/orderPay")
@RefreshScope
public class OrderPayController {
    @Autowired
    private IOrderInfoService orderInfoService;
    @Autowired
    private PayFeignApi payFeignApi;

   @RequestMapping("/alipay")
    public Result Resultalipay(String orderNo,int type){
        //根据订单参数进行非空校验
         if (StringUtils.isEmpty(orderNo) || StringUtils.isEmpty(type + "")){
              throw new BusinessException(SeckillCodeMsg.OPT_ERROR);
         }
         //根据类型去判断支付方式,线上支付还是积分支付
       if (OrderInfo.PAYTYPE_ONLINE.equals(type)){
           //线上支付
           String result = orderInfoService.payOnline(orderNo);
           return Result.success(result);
       }else if (OrderInfo.PAYTYPE_INTERGRAL.equals(type)){
              //积分支付
          orderInfoService.payIntergral(orderNo);
       }else {
           throw new BusinessException(SeckillCodeMsg.OPT_ERROR);
       }
       return Result.success("");
   }

    /**
     * 同步回调
     * 重新跳转到订单详情页面
     * 把支付宝回调的参数都会封装到map中
     */
    @Value("${pay.frontEndPayUrl}")
    private String frontEndPayUrl;
    @RequestMapping("/return_url")
    public void return_url(@RequestParam Map<String,String> params,HttpServletResponse response) throws IOException {
        System.out.println("同步回调的时间为"+new Date());
        //远程调用支付服务来进行验签操作
        Result<Boolean> result = payFeignApi.checkSign(params);//调用SDK验证签名
        //获取订单编号
        String orderNo = params.get("out_trade_no");
        //——请在这里编写您的程序（以下代码仅作参考）——
        if(result != null && !result.hasError()) {
           if (result.getData()){
            response.sendRedirect(frontEndPayUrl + orderNo);
           }else {
            response.sendRedirect("http://localhost/100z.html");
           }
        }
   }
   /*
   * 异步回调
   * 支付成功以后的业务逻辑
   * */
   @RequestMapping("/notify_url")
    public void notify_url(@RequestParam Map<String,String> params,HttpServletResponse response) throws IOException {
       System.out.println("异步回调的时间为" + new Date());
       PrintWriter out = response.getWriter();
       //验签操作
       Result<Boolean> result = payFeignApi.checkSign(params);
       //获取订单编号
       String orderNo = params.get("out_trade_no");
       if(result != null && !result.hasError()) {
           if (result.getData()){//验证成功
              orderInfoService.paySuccess(orderNo);
              out.println("success");
           }else {
              out.println("fail");
           }
       }
   }
   @RequestMapping("/refund")
    public Result refund(String orderNo){
       OrderInfo orderInfo = orderInfoService.find(orderNo);
       //对于订单编号进行非空校验
       if (StringUtils.isEmpty(orderNo)){
           throw new BusinessException(SeckillCodeMsg.OPT_ERROR);
       }

       //查询订单信息,根据订单支付类型进行不同退款流程
       if (OrderInfo.PAYTYPE_ONLINE.equals(orderInfo.getPayType())){
           //线上退款流程
           orderInfoService.refundOnline(orderInfo);
           return Result.success("");
       }else if (OrderInfo.PAYTYPE_INTERGRAL.equals(orderInfo.getPayType())){
          //积分退款流程
           orderInfoService.refundIntergral(orderInfo);
       }else {
           throw new BusinessException(SeckillCodeMsg.OPT_ERROR);
       }
       return Result.success("");
   }
}
